<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div>
            <div class="handle-box">
                <el-dialog :title="title" :visible.sync="dialogFormVisible">
                    <el-form :model="addForm">
                        <el-form-item label="角色名" :label-width="formLabelWidth">
                            <el-input v-model="addForm.name" placeholder="20字以内" auto-complete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="角色菜单" :label-width="formLabelWidth">
                            <el-tree ref="tree" :data="role_data" node-key="id" show-checkbox>
                            </el-tree>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取 消</el-button>
                        <el-button type="primary" @click="submit">确 定</el-button>
                    </div>
                </el-dialog>
                <el-button type="success" @click="add">添加角色</el-button>
            </div>
            <div>
                <el-table :data="roles" border fit>
                    <el-table-column prop="name" label="角色名" align="center">
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button @click="put(scope.row)">编辑</el-button>
                            <el-button @click="del(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="block">
                <el-pagination @current-change="handleCurrentChange" :current-page="num" layout="total,  prev, pager, next, jumper" :total="totalElements">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<script>
import { allRoles, putRole, delRole, addRole } from '../../api/server.js'
import { time_format } from '../../utils/date.js'
import { role_data } from '../../utils/role.js'
export default {
    data() {
        return {
            totalElements: 10,
            num: 1,
            roles: [],
            dialogFormVisible: false,
            addForm: {},
            title: '',
            formLabelWidth: '120px',
            role_data: role_data
        }
    },
    created() {
        this.get_role(1, 20)
    },
    computed: {
    },
    methods: {
        addArticle() {
            this.$router.push({
                path: '/articleEdit'
            })
        },
        handleCurrentChange(val) {
            this.num = val
            this.get_account(val, 10)
        },
        add() {
            this.addForm = {}
            this.title = '添加角色'
            this.dialogFormVisible = true
            return this.trees()
        },
        trees() {
            console.log('tree', this.$refs)
        },
        get_role(page, pageSize) {
            allRoles({ page, pageSize })
                .then(res => {
                    this.roles = res.result
                    this.totalElements = res.resultNum
                })
        },
        time(row) {
            if (row.last_login)
                return time_format(row.last_login, 'yyyy-MM-dd hh:mm:ss')
        },
        put(row) {
            this.addForm = row
            this.dialogFormVisible = true
            let choose = row.menu_ids.split(',')
            let _this = this
            _this.$nextTick(() => {
                _this.$refs.tree.setCheckedKeys(choose);
            })
        },
        del(id) {
            delRole({ id })
                .then(res => {
                    this.$message.success('删除成功')
                    this.get_role(1, 10)
                })
        },
        submit() {
            // console.log(this.$refs.tree.getCheckedKeys());
            // console.log('addform', this.addForm)
            let menuIds = this.$refs.tree.getCheckedKeys().join(',')
            let addfrom = this.addForm
            addfrom.menuIds = menuIds
            if (addfrom.id) {
                putRole(addfrom)
                    .then(res => {
                        this.$message.success('修改成功')
                        this.dialogFormVisible = false
                        this.get_role(1, 10)
                    })

            } else {
                addRole(addfrom)
                    .then(res => {
                        this.$message.success('添加成功')
                        this.dialogFormVisible = false
                        this.get_role(1, 10)
                    })
            }
        },
    }

}

</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>